/*******************************************************************************
tcsproc2.c

*******************************************************************************/
#ifdef _UNICODE

	typedef char CHAR;
	typedef CHAR* LPSTR;
	typedef const CHAR*	LPCSTR;

	typedef unsigned short TCHAR;
	typedef TCHAR*	LPTSTR;
	typedef const TCHAR* LPCTSTR;

	#ifndef _T
	#define _T(x)	L ## x
	#endif

#else //!UNICODE

	typedef char CHAR;
	typedef CHAR* LPSTR;
	typedef const CHAR*	LPCSTR;

	typedef CHAR TCHAR;
	typedef TCHAR*	LPTSTR;
	typedef const TCHAR* LPCTSTR;

	#ifndef _T
	#define _T(x)	x
	#endif

#endif//_UNICODE

#ifndef NULL
#define NULL 0
#endif


TCHAR _towupper(TCHAR tc)
{
	if(tc >= L'a' && tc <= L'z')
	{
		tc = L'A' + (tc - L'a');
	}
	return tc;
}

TCHAR _lower(TCHAR tc)
{
	if(tc >= _T('A') && tc <= _T('Z'))
	{
		tc = _T('a') + (tc - _T('A'));
	}
	return tc;
}

TCHAR _upper(TCHAR tc)
{
	if(tc >= _T('a') && tc <= _T('z'))
	{
		tc = _T('A') + (tc - _T('a'));
	}
	return tc;
}


#ifdef WIN32


#else //LINUX etc.

#ifdef _tcslen
#define _TCSPROC
#endif

#ifndef _TCSPROC

int _tcslen(LPCTSTR pSrc)
{
	LPCTSTR pTmp = pSrc;
	while(*pTmp) 
	{
		pTmp++;
	}
	return (int)(pTmp - pSrc);
}

LPTSTR _tcscpy(LPTSTR pDst, LPCTSTR pSrc)
{
	LPTSTR pRet = pDst;
	
	while(*pSrc)
	{
        *pDst++ = *pSrc++;
	}
	*pDst = 0;
	
	return pRet;
}

LPTSTR _tcsncpy(LPTSTR pDst, LPCTSTR pSrc, int nCount)
{
	LPTSTR pStart = pDst;
	
	while (nCount && ((*pDst++ = *pSrc++) != 0))    /* copy string */
	{
		nCount--;
	}
	
	if (nCount)                              /* pad out with zeroes */
	{
		while (--nCount)
			*pDst++ = 0;
	}
	
	return (pStart);
}

LPTSTR _tcscat(LPTSTR pDst, LPCTSTR pSrc)
{
	LPTSTR pTmp = pDst;
	
	while (*pTmp)
	{
		pTmp++;
	}
	
	while ((*pTmp++ = *pSrc++) != 0)
	{
		;
	}
	
	return (pDst);
}

int _tcsncmp(LPCTSTR pSrc, LPCTSTR pDst, int nCount)
{
	if (nCount == 0)
	{
		return(0);
	}
	
	while (--nCount && *pSrc && *pSrc == *pDst)
	{
		pSrc++;
		pDst++;
	}
	
	return ((int) (*pSrc - *pDst));
}


int _tcscmp(LPCTSTR pSrc, LPCTSTR pDst)
{
	int pRet = 0 ;
	
	while (((pRet = (int) (*pSrc - *pDst)) == 0) && *pDst)
	{
		++pSrc, ++pDst;
	}
	
	if (pRet < 0)
	{
		pRet = -1 ;
	}
	else if (pRet > 0)
	{
		pRet = 1 ;
	}
	
	return (pRet);
}

int _tcsicmp(LPCTSTR pSrc, LPCTSTR pDst)
{
	int ret = _towupper(*pSrc) - _towupper(*pDst);
	
	while(ret == 0 && *pDst)
	{
		++pSrc, ++pDst;
		
		ret = _towupper(*pSrc) - _towupper(*pDst);
	}
	
	if (ret < 0)
	{
		ret = -1 ;
	}
	else if (ret > 0)
	{
		ret = 1 ;
	}
	
	return ret;
}

int _tcsnicmp(LPCTSTR pSrc, LPCTSTR pDst, int nCount)
{
	int ret = 0;
	while(nCount > 0 && ret == 0 && *pDst)
	{
		ret = _towupper(*pSrc) - _towupper(*pDst);
		pSrc++;
		pDst++;
		
		nCount--;
	}
	
	if (ret < 0)
	{
		ret = -1 ;
	}
	else if (ret > 0)
	{
		ret = 1 ;
	}
	
	return ret;
}

LPTSTR _tcsmove(LPTSTR pDst, LPCTSTR pSrc, int nCount)
{
	LPTSTR pRet = pDst;
	
	if (pDst <= pSrc || pDst >= (pSrc + nCount)) 
	{
		while (nCount--) 
		{
			*pDst = *pSrc;
			pDst = pDst + 1;
			pSrc = pSrc + 1;
		}
	}
	else 
	{
		pDst = pDst + nCount - 1;
		pSrc = pSrc + nCount - 1;
		
		while (nCount--) 
		{
			*pDst = *pSrc;
			pDst = pDst - 1;
			pSrc = pSrc - 1;
		}
	}
	
	return (pRet);
}


long _ttol(LPCTSTR cp)
{
	long result = 0;
	int sign = 1;
	
	if ( *cp == L'-' )
	{
		sign = -1;
		cp++;
	}
	
	while(*cp)
	{
		if(*cp >= L'0' && *cp <= L'9')
		{
			result = result*10 + (*cp - L'0');
		}
		else
		{
			break;
		}
		cp++;
	}
	return result * sign;
}


int _ttoi(LPCTSTR cp)
{
	int result = 0;
	int sign = 1;
	
	if ( *cp == L'-' )
	{
		sign = -1;
		cp++;
	}
	
	while(*cp)
	{
		if(*cp >= L'0' && *cp <= L'9')
		{
			result = result*10 + (*cp - L'0');
		}
		else
		{
			break;
		}
		cp++;
	}
	return result * sign;
}


LPTSTR _tcschr(LPCTSTR tcs, TCHAR tc)
{
	do
	{
		if(*tcs == tc)
			return (LPTSTR)tcs;
		
	}while (*tcs++ != L'\0');
	
	return NULL;
}


int _tcscspn(LPCTSTR tcs, LPCTSTR reject)
{
	int count = 0;
	
	while(*tcs != L'\0')
	{
		if(_tcschr (reject, *tcs++) == NULL)
			++count;
		else
			return count;
	}
	return count;
}

int _tcsspn(LPCTSTR str, LPCTSTR accept)
{
	LPTSTR s;
	LPTSTR t;
	s=(LPTSTR)str;
	
	do {
		t=(LPTSTR)accept;
		while (*t) { 
			if (*t==*s) 
				break;
			t++;
		}
		if (!*t) 
			break;
		s++;
	} while (*s);
	return s-str; /* nr of wchars */
}

LPTSTR _tcsstr(LPCTSTR pSrc, LPCTSTR pDst)
{
	LPCTSTR x;
	LPCTSTR y;
	LPCTSTR c;
	
	x = pSrc;
	
	while(*x)
	{
		if(*x == *pDst)
		{
			y = x;
			c = pDst;
			while(*y && *c && *y==*c)
			{ 
				c++;
				y++; 
			}
			if(!*c)
				return (LPTSTR)x;
		}
		x++;
	}
	return NULL;
}

LPTSTR _tcslwr(LPTSTR pStr)
{
	LPTSTR p = pStr;
	while(*p)
	{
		*p = _lower(*p);
		p++;
	}
	return pStr;
}

LPTSTR _tcsurp(LPTSTR pStr)
{
	LPTSTR p = pStr;
	while(*p)
	{
		*p = _upper(*p);
		p++;
	}
	return pStr;
}

#endif //_TCSPROC

#endif //_OS_ dependent


LPCTSTR _tcsistr(LPCTSTR pSrc, LPCTSTR pDst)
{
	LPCTSTR x;
	LPCTSTR y;
	LPCTSTR c;
	
	x = pSrc;
	
	while(*x)
	{
		if(_lower(*x) == _lower(*pDst))
		{
			y = x;
			c = pDst;
			while(*y && *c && _lower(*y)==_lower(*c))
			{ 
				c++;
				y++; 
			}
			if(!*c)
				return (LPTSTR)x;
		}
		x++;
	}
	return NULL;
}

LPTSTR _tcscpyA2T(LPTSTR pDst, LPCSTR pSrc)
{
	LPTSTR pRet = pDst;
	
	while(*pSrc)
	{
        *pDst++ = (TCHAR)*pSrc++;
	}
	*pDst = 0;
	
	return pRet;
}


LPTSTR _tcscatA2T(LPTSTR pDst, LPCSTR pSrc)
{
	LPTSTR pTmp = pDst;
	
	while (*pTmp)
	{
		pTmp++;
	}
	
	while ((*pTmp++ = (TCHAR)*pSrc++) != 0)
	{
		;
	}
	
	return (pDst);
}

int _tcsnicmpA2T(LPCTSTR pSrc, LPCSTR pDst, int nCount)
{
	int ret = 0;
	while(nCount > 0 && ret == 0 && *pDst)
	{
		ret = _towupper(*pSrc) - _towupper((TCHAR)*pDst);
		pSrc++;
		pDst++;
		
		nCount--;
	}
	
	if (ret < 0)
	{
		ret = -1 ;
	}
	else if (ret > 0)
	{
		ret = 1 ;
	}
	
	return ret;
}
